<script setup>
import { ref, onMounted } from 'vue'
// import api from '../../api/payments_api.js'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs';

// 定义响应式数据
const payments = ref([]) // 支付记录列表
const loading = ref(false) // 加载状态
const total = ref(0);
//绑定查询数据
const searchObj=ref({
  current:1,
  limit:10,
  order_id:'',
  amount:'',
  payment_id:'',
  payment_time:''
});

//定义方法
const handleSizeChange=()=>{
  search();
}
const handleCurrentChange=()=>{
  search();
}

const formatTime = (time) => {
  return dayjs(time).format('YYYY-MM-DD HH:mm:ss');
};

/**
 * 获取支付记录
 */
const fetchPayments = async () => {
  try {
    loading.value = true
    const res = await api.pageuser(searchObj.value);
    payments.value = res.data;
    total.value=res.count;
  } catch (error) {
    ElMessage.error('获取支付记录失败')
  } finally {
    loading.value = false
  }
}

/**
 * 获取支付方式图标
 * @param {String} method 支付方式
 * @returns {String} 图标组件名称
 */
const getMethodIcon = (method) => {
  return method === 'alipay' ? 'Alipay' : 'Wechat'
}

/**
 * 获取支付状态颜色
 * @param {String} status 支付状态
 * @returns {String} 颜色值
 */
const getStatusColor = (status) => {
  return status === 'success' ? '#2ec4b6' : '#f72585'
}

onMounted(() => {
  fetchPayments()
});
</script>

<template>
  <div class="payments-container">
    <h1 class="page-title">支付记录</h1>

    <!-- 空支付记录状态 -->
    <div v-if="payments.length === 0" class="empty-payments">
      <el-empty description="暂无支付记录"></el-empty>
    </div>

    <!-- 支付记录列表 -->
    <div v-else class="payments-list">
      <!-- 遍历支付记录 -->
      <div
          v-for="payment in payments"
          :key="payment.payment_id"
          class="payment-card"
      >
        <!-- 支付记录头部 -->
        <div class="payment-header">
          <div class="payment-id">支付单号: {{ payment.payment_id }}</div>
          <div class="payment-time">{{ formatTime(payment.payment_time) }}</div>
        </div>

        <!-- 支付记录主体 -->
        <div class="payment-body">
          <!-- 支付方式 -->
          <div class="payment-method">
            <el-icon :size="24">
              <component :is="getMethodIcon(payment.method)" />
            </el-icon>
            <span>{{ payment.method === 'alipay' ? '支付宝支付' : '微信支付' }}</span>
          </div>

          <!-- 支付金额 -->
          <div class="payment-amount">
            ¥{{ payment.amount }}
          </div>

          <!-- 支付状态 -->
          <div
              class="payment-status"
              :style="{ color: getStatusColor(payment.status) }"
          >
            {{ payment.status === 'success' ? '支付成功' : '支付失败' }}
          </div>
        </div>

        <!-- 支付记录底部 -->
        <div class="payment-footer">
          <div class="order-info">
            关联订单: {{ payment.order_id }}
          </div>
          <!-- 查看订单详情按钮 -->
<!--          <el-button-->
<!--              type="text"-->
<!--              size="small"-->
<!--              @click="$router.push(`/orderDetail/${payment.order_id}`)"-->
<!--          >-->
<!--            查看订单详情-->
<!--          </el-button>-->
        </div>

        </div>
      </div>
    <!-- 分页控件 -->
    <div class="pagination-container">
      <el-pagination
          v-model:current-page="searchObj.current"
          v-model:page-size="searchObj.limit"
          :page-sizes="[10, 20, 30, 40]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          class="pagination"
      />
    </div>
  </div>
</template>

<style scoped>
/* 支付记录容器样式 */
.payments-container {
  padding-top: 100px;
  max-width: 800px;
  margin: 0 auto;
}

/* 页面标题样式 */
.page-title {
  text-align: center;
  color: #4361ee;
  margin-bottom: 30px;
  font-size: 28px;
}

/* 空支付记录样式 */
.empty-payments {
  margin-top: 50px;
  text-align: center;
}

/* 支付记录列表样式 */
.payments-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

/* 支付记录卡片样式 */
.payment-card {
  background: white;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: transform 0.3s ease;
}

/* 支付记录卡片悬停效果 */
.payment-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* 支付记录头部样式 */
.payment-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px dashed #eee;
}

/* 支付单号样式 */
.payment-id {
  font-weight: bold;
  color: #333;
}

/* 支付时间样式 */
.payment-time {
  color: #666;
  font-size: 14px;
}

/* 支付记录主体样式 */
.payment-body {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

/* 支付方式样式 */
.payment-method {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #4361ee;
  font-weight: bold;
}

/* 支付金额样式 */
.payment-amount {
  font-size: 24px;
  font-weight: bold;
  color: #f72585;
}

/* 支付状态样式 */
.payment-status {
  font-weight: bold;
}

/* 支付记录底部样式 */
.payment-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 15px;
  border-top: 1px dashed #eee;
}

/* 关联订单信息样式 */
.order-info {
  color: #666;
  font-size: 14px;
}

/* 响应式设计 - 移动端适配 */
@media (max-width: 600px) {
  .payment-header {
    flex-direction: column;
    gap: 5px;
  }

  .payment-body {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }

  .payment-footer {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
}

/* 分页容器样式 */
.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
  padding-right: 10px;
}
</style>